App({
  // 声明全局数据
  globalData: {
    isAuthor: false, // 是否授权
    openId: null, //用户号
    isLogin: true, // 是否登录
    isRegister: true, // 是否注册
    authCode: null, // 授权信息
    userInfo: null, // 用户信息，到时候登录成功会将所有用户信息放在这个字段，比如手机号，身份证，实名信息等
  },

  // 声明全局方法
  getUserInfo() {

    let that = this
    return new Promise((resolve, reject) => {
      if (that.globalData.userInfo) resolve(that.globalData.userInfo)
      // 调用用户授权 API
      my.getAuthCode({
        scopes: ['auth_user'],
        success: authcode => {

          console.log("-=-=", authcode);
          that.globalData.isAuthor = true
          that.globalData.authCode = authcode.authCode;
          console.log("-=-=", that.globalData.authCode);
          // 调用获取用户信息 API
          my.getAuthUserInfo({
            success: res => {
              that.globalData.userInfo = res;
              that.globalData.userInfo.authCode = authcode.authCode;
              resolve(that.globalData.userInfo);
            },
            fail: err => {
              reject(err)
              my.showToast({
                type: 'fail',
                content: err.errorMessage,
                duration: 1000
              })
            },
          })

        },
        fail: err => {
          reject(err)
          my.showToast({
            type: 'fail',
            content: err.errorMessage,
            duration: 1000
          })
        },

      })
    })
  },
  async getOpenID() {

    let ropenides = my.getStorageSync({
      key: 'openid'
    });
    console.log("003030330303", ropenides)
    if (ropenides.data != null) {
      console.log("003030330303")
      // return;
    }


    // const akcontext = await my.cloud.createCloudContext({
    //   env: 'env-00jxgeypg2iy'
    // });
    // await akcontext.init();
    // var ress = await my.fncontext.callFunction({
    //   name: "baiduOcr"
    // });
    // console.log("resres", ress);




    const context = await my.cloud.createCloudContext({
      env: 'env-00jxgeypg2iy'
    });
    await context.init();
    var res = await my.fncontext.callFunction({
      name: "getOpenID"
    });
    console.log("openid====", res.result.OPENID);

    let rees = my.setStorageSync({
      key: 'openid',
      data: res.result.OPENID
    });
    console.log(rees);
    if (rees.success) {
      console.log("执行成功");
    }



  },
  async onLaunch(options) {











    // 第一次打开
    // options.query == {number:1}
    console.info('App onLaunch');
    const context = my.cloud.createCloudContext({
      env: 'env-00jxgeypg0jg', //注意，此处换成你自己的环境ID
    })
    context.init();
    this.context = context;

    console.log("小程序启动")

    try {
      //初始化function调用，修改为自己的环境 ID
      const context = await my.cloud.createCloudContext({
        env: 'env-00jxgeypg2iy'
      });
      await context.init();
      my.fncontext = context;
      this.getOpenID();
      console.log("初始化函数上下文成功");

    } catch (error) {
      console.log("初始化函数上下文失败");
      console.log(error);
    }



    //使用function的方式进行初始化，两中方式取其中一个方式就可以，用户可以使用自己合适的方式。
    my.getCloudContext = async function () {
      if (my.fncontext) {
        return my.fncontext;
      } else {
        const context = await my.cloud.createCloudContext({
          env: 'env-00jxgeypg2iy'
        });

        await context.init();
        my.fncontext = context;
      }
      return my.fncontext;
    }
  },
  onShow(options) {},



});